

public class seek {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long start, stop;
		// stop = System.nanoTime();
		// start = System.nanoTime();
		// System.out.println("TEMPO: " + (stop - start) + " nanosegs");
		int size = 100000;
		int[] v = CreateSortVector.getVct(size);
		for (int i = 0; i < v.length; i++) {
			System.out.print(v[i] + ",");
		}
		System.out.println();
		// sequentialSeek(34,v,size);
		start = System.nanoTime();
		System.out.print("K igual a " + sequentialSeek(34, v, size));
		stop = System.nanoTime();
		System.out.println("  Sequencial TEMPO: " + (stop - start) + " nanosegs");

		start = System.nanoTime();
		System.out.print("K igual a " + binarySeek(34, v, size));
		stop = System.nanoTime();
		System.out.println("  Binario TEMPO: " + (stop - start) + " nanosegs");
	}

	public static int sequentialSeek(int element, int[] v, int size) {
		int i = 0, k = -1;
		while ((i < size) && (k == -1)) {
			if (element == v[i]) {
				k = i;
			} else if (v[i] < element) {
				i = i + 1;
			} else {
				k = -2;
			}
		}
		return k;
	}

	public static int binarySeek(int element, int v[], int size) {
		int start = 0, end = size - 1, mid, k = -1;
		while ((start <= end) && (k == -1)) {
			mid = (start + end) / 2;
			if (element == v[mid]) {
				k = mid;
			} else if (element < v[mid]) {
				end = mid - 1;
			} else {
				start = mid + 1;
			}
		}
		return k;
	}

}
